<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/static/jquery-easyui/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/static/jquery-easyui/themes/icon.css"/>">
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/jquery-1.6.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/plugins/jquery.panel.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/plugins/jquery.datagrid.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/plugins/jquery.messager.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/ext-for-framework.js.jsp"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/locale/easyui-lang-zh_CN.js"/>"></script>
<script>
			function initList(listDisplayEId) {
				$('#' + listDisplayEId).datagrid(
						{
							url:"<c:url value='/datadictionary/datadictionarylist.do'/>",
							queryParams: {
								codeType: $('#codeType').combobox('getValue')
							},
							nowrap : false,
							striped : true,
							collapsible : true,
							remoteSort : true,
							notSelectByRow : false,
							/* frozenColumns : [ [ {
								field : 'ids',
								checkbox : true
							} ] ], */
							toolbar : [
									{
										id : listDisplayEId + '_btn_add',
										text : '新增',
										iconCls : 'icon-add',
										handler : function() {
												
												var lastIndex = $('#' + listDisplayEId).datagrid('getRows').length;
												
													 $('#' + listDisplayEId).datagrid('endEdit', lastIndex);
														$('#' + listDisplayEId).datagrid('appendRow',{
															phCodeId:'',
															codeId:'',
															codeName:'',
															codeOder:'',
															describ:'',
															codeTypeId:$('#codeType').combobox('getValue'),
															isActive:''
														}); 
														
														var lastIndex = $('#' + listDisplayEId).datagrid('getRows').length-1;
														$('#' + listDisplayEId).datagrid('selectRow', lastIndex);
														var selected = $('#listDisplay').datagrid("getSelected");
														if(selected.codeTypeId==null||selected.codeTypeId==""){
															$('#' + listDisplayEId).datagrid('rejectChanges');
															$.messager.alert("提示", "请选择编码类型后添加编码", 'warning');
														}else{
															
															$('#' + listDisplayEId).datagrid('beginEdit', lastIndex);
														}
										}
									},'-',{
					text:'撤销',
					iconCls:'icon-undo',
					handler:function(){
						$('#' + listDisplayEId).datagrid('rejectChanges');
						
					}
				},
									{
										id : listDisplayEId + '_btn_del',
										text : '删除',
										iconCls : 'icon-cancel',
										handler : function() {
					var row = $('#'+listDisplayEId).datagrid('getSelections');		
					var str = "phCodeId='"+row[0]['phCodeId']+"'";
					if(row.length == 0){
						$.messager.alert('提示','请选择一条记录操作！','info');
						return;
					}else{
						$.messager.confirm('提示', '是否删除该条记录？', function(r){
							if (r){			
								$.ajax({type:'post',url:"<c:url value='/datadictionary/deleteCode.do'/>",data:str,dataType:"json",//设置查询参数及提交路径
							 		success:function(data){
										if(data.success){
											$.messager.alert("提示", "删除成功！", 'info');
											$('#listDisplay').datagrid('reload',{
												codeType: $('#codeType').combobox('getValue')
											});
										}else{
											$.messager.alert("提示", data.msg, 'warning');
										}
									},
									error:function(){
										$.messager.alert("错误", "系统错误，请联系管理员！", 'error');
									}
								});
							}
						});
					}
					$('#'+listDisplayEId).datagrid('clearSelections');
				
										}
									}],
							onLoadError:function(){
								$.messager.alert('错误','获取数据失败，请与管理员联系！','error');
							} ,
							onClickRow:function(rowIndex){
								$(".myrad").prop("checked",false);
								$(".myrad").eq(rowIndex).prop("checked",true);
							} 
						});
			};
			function formatRadio(value,rowData,rowIndex){
				return value="<input type='radio' class='myrad'  name='radioDictionary' />";
			}
			 
			$(document).ready(function() {
				initList('listDisplay');
				initCodeType();
			});
			function save(index){
				$('#listDisplay').datagrid('endEdit', index); 
				//数据验证
				var selected = $('#listDisplay').datagrid("getSelected");
				if(selected==null){
					$.messager.alert("提示", "保存失败！", 'error');
					return;
				}else{
					if(selected.codeId==null||selected.codeId==""){
						$.messager.alert("提示", "编码不能为空！", 'warning');
						return;
					}
					if(selected.codeName==null||selected.codeName==""){
						$.messager.alert("提示", "编码名称不能为空！", 'warning');
						return;
					}
					if(selected.codeOder==null||selected.codeOder==""){
						$.messager.alert("提示", "序号不能为空！", 'warning');
						return;
					}
					if(selected.codeOder==null||selected.codeOder==""){
						$.messager.alert("提示", "序号不能为空！", 'warning');
						return;
					}
				} 
				
				//获取表格行数
				//$('#listDisplay').datagrid('endEdit', index);  
				var rows = $('#listDisplay').datagrid('getRows');
                var allData = rows[index];
                    //var allData = $('#listDisplay').datagrid('getData');//获取表格所有数据
                    var str = "rows="+JSON.stringify(allData);
					$.ajax({type:'post',url:"<c:url value='/datadictionary/saveCode.do'/>",data:str,dataType:"json"
						,success:function(data){
							if(data.success){
								$.messager.alert("提示", "保存成功！", 'info');
								$('#listDisplay').datagrid('reload',{
									codeType: $('#codeType').combobox('getValue')
								});

							}else{
								$.messager.alert("提示", "保存失败！", 'warning');
							}
						}
						,error:function(){
							$.messager.alert("提示", "提交失败！", 'error');
						}
					});
                
			}
			function editrow(index){
				$('#listDisplay').datagrid('beginEdit', index);
				var value =$('.datagrid-editable-input').eq(0).val();
				$('.datagrid-editable-input').eq(0).parent().html(value);
			}
			
			//角色状态展示1：可用；0为锁定
			function formatDGStateField(value,row,index,field){
				if(!field){
					field = this.field;
				}
				var fields = field.split('.');
				var fieldValue = row;
				for(var i=0;i < fields.length;i++){
					if(fieldValue[fields[i]]==0){
					 fieldValue = "停用";
					   }else if(fieldValue[fields[i]]==1){
						  fieldValue = "可用"
						 }
				}
				return fieldValue;
			}
			function formatOptionField(value,row,index,field){
			
				return "<span style='color:red'><a href='#' onclick='save("+index+",this)'>保存</a>&nbsp<a href='#' onclick='editrow("+index+",this)'>修改</a></span>";
			}
			function searchDGData(){
				$('#listDisplay').datagrid('load',{
					codeType: $('#codeType').combobox('getValue')
				});

			} 
			function codeTypeFormatter(value){
				for(var i=0; i<codeTypes.length; i++){
					if (codeTypes[i].codeTypeId == value) return codeTypes[i].name;
				}
				return value;
			}
			 function initCodeType(){
				$('#codeType').combobox({
						url:"<c:url value='/datadictionary/codeTypelist.do'/>",
						valueField:'codeTypeId',
						textField:'codeTypeName',
						panelWidth:'220',
						onSelect:function(record){ 
							 $('#codeType').value = record.codeTypeId;
							 codeTypeIdVal = record.codeTypeId;
						},  
						 onLoadSuccess: function (data) {
							 if (data) {
				                $('#codeType').combobox('setValue',data[0].codeTypeId);
				            }
				        }

				});
			} 
		</script>
</head>
<body>
	<form id="searchForm" name="searchForm" method="post">

		<table width="98%">
			<tr>
				<td>
					<div style="height: auto;width:1180px; background: #fafafa; padding: 5px;"
						class="easyui-panel" title="编码 查询" iconCls="icon-search"
						collapsible="true" minimizable="false" maximizable="false"
						closable="false">
						

						<div>
							<label for="StartDate">编码类型:</label> <input
								class="easyui-combobox" type="text" name="codeType"
								id="codeType"></input> &nbsp;&nbsp;
							&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input
								type="button" value="查询" onclick="searchDGData()">


						</div>

					</div>
				</td>
			</tr>
			<tr style="height: 12px;"></tr>
			<tr>
				<td>
					<table id="listDisplay" title="编码" class="easyui-datagrid" iconCls="icon-list"
						singleSelect="true" idField="id" loadMsg="请稍候......"
						pagination="true" pageSize="10" pageList="[10,20,30,40,50,100]" sortName="codeOder"
						rownumbers="true" sortOrder="asc"
					>
						<thead>
							<tr>
								<%--<td field="fhCodeId" width="250" sortable="true"
						formatter="formatDGField" >
						fhCodeId
					</td>--%>
								<th field="radioRole" width="25" align="center" formatter="formatRadio"  >
								<!-- <th field="CodeType" id="codeType" hidden="true" value="1"> -->
								<th field="phCodeId" hidden="true"></th>
								<th field="codeId" class="codeId" width="80" sortable="true" editor="text">
									编码编号</th>

								<th field="codeName" width="80" sortable="true" editor="text">
									编码名称</th>
								<th field="codeOder" width="80" sortable="true" editor="text">
									序号</th>
								<th field="describ" width="80" sortable="true" editor="text">
									描述</th>
								<th field="codeTypeId" width="80" sortable="true">编码类型</th>
								<!-- 
					<th field="isActive" width="80" formatter="formatIsActive" sortable="true" editor="{type:'combobox',options:{required:true,valueField:'isActive',textField:'isa',data:[{isAcitve:'1','isa':'可用'},{isAcitve:'0','isa':'不可用'}]}}">

						是否可用
					</th>
					 -->
								<th field="isActive" width="60" align="center"
									formatter="formatDGStateField"
									editor="{type:'checkbox',options:{on:'1',off:'0'}}">是否可用</th>

								<th field="button" width="260" align="center"
									formatter="formatOptionField">操作</th>

							</tr>
						</thead>
					</table>
			</td>
			</tr>
			</table>
			</form>
</body>
</html>